package com.ahu;

import com.ahu.Repository.RepairCurRepository;
import com.ahu.Repository.RepairLogRepository;
import com.ahu.domain.RepairCur;
import com.ahu.domain.RepairLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by qx on 2018/1/9.
 */
@Component
public class Task {
    private RepairCurRepository repairCurRepository;
    private RepairLogRepository repairLogRepository;
    @Autowired
    public Task(RepairCurRepository repairCurRepository, RepairLogRepository repairLogRepository) {
        this.repairCurRepository = repairCurRepository;
        this.repairLogRepository = repairLogRepository;

    }
    @Scheduled(cron = "0 0 12 * * ?")
    public void aTask(){
        List<RepairCur> repairCurList = repairCurRepository.findAllByRepairStatus("已完工");
        repairCurList.forEach(repairCur->{
            RepairLog repairLog = new RepairLog();
            repairLog.setNumber(repairCur.getNumber());
            repairLog.setRepairAddress(repairCur.getRepairAddress());
            repairLog.setRepairContext(repairCur.getRepairContext());
            repairLog.setRepairTime(repairLog.getRepairTime());
            repairLog.setRepairStatus(repairCur.getRepairStatus());
            repairLog.setCreateTime(repairCur.getCreateTime());
            repairLog.setFinishTime(repairCur.getFinishTime());
            repairLogRepository.save(repairLog);
            repairCurRepository.delete(repairCur);
        });
    }
}
